//
//  IndexViewController.m
//  BosidengIphone
//
//  Created by  on 11-12-28.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "IndexViewController.h"
#import "ProductData.h"
#import "NewsViewController.h"
#import "ProductViewController.h"
#import "MyShowIndexViewController.h"
#import "VideoListViewController.h"
#import "StoreViewController.h"
#import "SuggestViewController.h"
#import "AboutViewController.h"
#import "ProductDetailViewController.h"
#import "MainViewController.h"
#import "AppDelegate.h"

@implementation IndexViewController
@synthesize products;
@synthesize pageControl;
@synthesize name;
@synthesize introduction;
@synthesize topMenuViewController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc {
    [topMenuViewController release];
    [introduction release];
    [name release];
    [pageControl release];
    [products release];
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    pageControl.numberOfPages = [products count];
    
    TopMenuViewController *viewController = [[TopMenuViewController alloc] initWithNibName:@"TopMenuViewController" bundle:nil];
    viewController.delegate = self;
    viewController.topMenus = [NSArray arrayWithObjects:
                               [AppDelegate getLanguageString:@"news_label_title"],
                               [AppDelegate getLanguageString:@"product_label_title"],
                               [AppDelegate getLanguageString:@"show_label_title"],
                               [AppDelegate getLanguageString:@"video_label_title"],
                               [AppDelegate getLanguageString:@"store_label_title"],
                               [AppDelegate getLanguageString:@"suggest_label_title"],
                               [AppDelegate getLanguageString:@"about_label_title"],
                               nil];
    self.topMenuViewController = viewController;
    [viewController release];
    
    UIView *topMenuView = topMenuViewController.view;
    topMenuView.frame = CGRectMake(0, -460, 320, 460);
    [self.view addSubview:topMenuView];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    self.pageControl = nil;
    self.name = nil;
    self.introduction = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)showMenuAnimation:(UIView*)view {
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:0.5];
	[UIView setAnimationDelay:0.0];
    CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 460);
    view.transform = transform;
	[UIView commitAnimations];
}

- (IBAction)showTopMenu:(id)sender {
    [self showMenuAnimation:topMenuViewController.view];
}

#pragma mark - PhotoScrollViewDelegate

- (NSInteger)imageCount {
    return [products count];
}

- (UIView *)configureView:(UIView *)page atIndex:(NSInteger)index {
    /*if (page == nil) {
        ProductData *data = [products objectAtIndex:index];
        NSString *url = [data getHeadImageUrl];
        page = [[[SyncImageView alloc] initWithUrl:url andRequestQueue:requestQueue] autorelease];
    }
    return page;
    */
    ProductData *data = [products objectAtIndex:index];
    NSString *url = [data getHeadImageUrl];
    if (page == nil) {
        page = [[[SyncImageView alloc] init] autorelease];
    }
    [(SyncImageView *)page loadImage:url andRequestQueue:requestQueue];
    return page;
}

- (void)changePageIndex:(NSInteger)index {
    pageControl.currentPage = index;
    ProductData *data = [products objectAtIndex:index];
    name.text = [NSString stringWithFormat:@"%@ %@", data.name, data.item];
    introduction.text = data.introduction;
}

- (void)selectPageIndex:(NSInteger)index {
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    ProductViewController *viewController = [[ProductViewController alloc] initWithNibName:@"ProductViewController" bundle:nil];
    viewController.products = [ProductData getAllItems];
    [delegate showMainView:viewController];
    [viewController release];
    self.mainViewController = viewController.mainViewController;
    [(MainViewController *)mainViewController changeTitle:[AppDelegate getLanguageString:@"product_label_title"]];
    ProductData *data = [products objectAtIndex:index];
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(showDetailView:) userInfo:data repeats:NO];
}

- (void)showDetailView:(NSTimer *)timer {
    ProductData *data = timer.userInfo;
    ProductDetailViewController *viewController = [[ProductDetailViewController alloc] initWithNibName:@"ProductDetailViewController" bundle:nil];
    viewController.product = data;
    viewController.brand = [BrandData getItemByID:viewController.product.brandID];
    [(MainViewController *)mainViewController pushViewController:viewController];
    [viewController release];
}

#pragma mark - TopMenuViewDelegate

- (void)changeViewController:(NSInteger)index andTitle:(NSString *)title {
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    if (index == 0) {
        NewsViewController *viewController = [[NewsViewController alloc] initWithNibName:@"NewsViewController" bundle:nil];
        [delegate showMainView:viewController];
        [viewController release];
        self.mainViewController = viewController.mainViewController;
    } else if (index == 1) {
        ProductViewController *viewController = [[ProductViewController alloc] initWithNibName:@"ProductViewController" bundle:nil];
        viewController.products = [ProductData getAllItems];
        [delegate showMainView:viewController];
        [viewController release];
        self.mainViewController = viewController.mainViewController;
    } else if (index == 2) {
        MyShowIndexViewController *viewController = [[MyShowIndexViewController alloc] initWithNibName:@"MyShowIndexViewController" bundle:nil];
        [delegate showMainView:viewController];
        [viewController release];
        self.mainViewController = viewController.mainViewController;
    } else if (index == 3) {
        VideoListViewController *viewController = [[VideoListViewController alloc] initWithNibName:@"VideoListViewController" bundle:nil];
        [delegate showMainView:viewController];
        [viewController release];
        self.mainViewController = viewController.mainViewController;
    } else if (index == 4) {
        StoreViewController *viewController = [[StoreViewController alloc] initWithNibName:@"StoreViewController" bundle:nil];
        [delegate showMainView:viewController];
        [viewController release];
        self.mainViewController = viewController.mainViewController;
    } else if (index == 5) {
        SuggestViewController *viewController = [[SuggestViewController alloc] initWithNibName:@"SuggestViewController" bundle:nil];
        [delegate showMainView:viewController];
        [viewController release];
        self.mainViewController = viewController.mainViewController;
    } else if (index == 6) {
        AboutViewController *viewController = [[AboutViewController alloc] initWithNibName:@"AboutViewController" bundle:nil];
        viewController.mData = [BrandData getFirstItem];
        [delegate showMainView:viewController];
        [viewController release];
        self.mainViewController = viewController.mainViewController;
    }
    [(MainViewController *)mainViewController changeTitle:title];
}

@end
